
import { ECSImpl } from '../../../3rd/ECS/impl/ECSImpl';
import { ButtonEven } from '../../../Bridge/UIBridge';
import { Vector3 } from '../../../Bridge/Vec';
import { App } from '../../../Framework/Core/App';
import { CtrlBase } from '../../../Framework/Core/CtrlBase';
import { SceneManager } from '../../../Framework/Manager/SceneManager';
import Util from '../../../Framework/Util/Util';
import { GameComponent, GameState } from '../../ECSLogic/components/GameComponent';
import { InputComponent, UserCommand } from '../../ECSLogic/components/InputComponent';
import { UserInputEvent } from '../../ECSLogic/events/GameEvent';
import { GameSystem } from '../../ECSLogic/systems/gamesystem';
import CtrlName from '../../UIDefine';
import { HomeView } from './HomeView';




/**
 * 登录页面控制器
 */
export class HomeCtrl extends CtrlBase   
{

    public declare view:HomeView

    // private ecs = createECSEnv();

    private ecs = new ECSImpl()

    public  OnAwake(arg:any)
    {
        Util.Log("HomeCtrl OnAwake");
        
    }

    public  OnCreate()
    {

        ButtonEven.SetClickEvent(this.view.backNode,()=>
        {
            this.testBack();
        });


        ButtonEven.SetClickEvent(this.view.pauseNode,()=>
        {
           this.PauseEcsSys()
        });

        ButtonEven.SetClickEvent(this.view.startNode,()=>
        {
           this.StartEcsSys()
        });

        App.Instance.sceneMgr.LoadScene("game",(scene,error)=>
        {

            this.Preload();
        });


        ButtonEven.SetClickEvent(this.view.rightBtn,()=>
        {
            this.testRight();
        });

        ButtonEven.SetClickEvent(this.view.leftBtn,()=>
        {
            this.testLeft();
        });

        ButtonEven.SetClickEvent(this.view.jumpBtn,()=>
        {
            this.testJump();
        });

    }

    testJump()
    {
        this.ecs.events.push(new UserInputEvent(UserCommand.up));
    }

    testLeft()
    {
        this.ecs.events.push(new UserInputEvent(UserCommand.left));
    }

    testRight()
    {
        this.ecs.events.push(new UserInputEvent(UserCommand.right));
    }


    /**
     * 暂停游戏
     */
    PauseEcsSys()
    {
        
        var gameComp =  this.ecs.components.get(GameComponent);
        gameComp.gameState = GameState.GamePause
    }

     /**
     * 暂停游戏
     */
      StartEcsSys()
      {
          
          var gameComp =  this.ecs.components.get(GameComponent);
          gameComp.gameState = GameState.GameStart
      }

    public Preload()
    {

        let preloadCfg: string[] = ["player", "star", "barrier"];


        App.Instance.preloadSystem.Load(preloadCfg,()=>{
            this.CreateSystem();
        });

       
    }

    public CreateSystem()
    {        
        console.log("CreateSystem");
        
        this.ecs.systems.add(new GameSystem());
        this.ecs.start();
    }

    testBack()
    {
        App.Instance.sceneMgr.LoadScene("login",(scene,error)=>
        {
            App.Instance.ctrlMgr.Back();
        });

       
    }

    public OnShow()
    {
        Util.Log("OnShow");
        this.view.ShowOk()
        
    }

    public Update(deltaTime:number)
    {
        this.ecs.update(deltaTime);
    }

    OnReMove()
    {   
        this.ecs.systems.clear();
    }
    
}


